home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.eunet.fi!nullnet!ichaos!jlaiho
- From: jlaiho@ichaos.nullnet.fi (Juha Laiho)
- Subject: Function pointers; example code
- Content-Type: text/plain; charset=ISO-8859-1
- Content-Transfer-Encoding: 8bit
- Organization: NullNet r.y.
- Message-ID: <DMwzBL.HDt@ichaos.nullnet.fi>
- Mime-Version: 1.0
- Date: Sat, 17 Feb 1996 09:50:09 GMT
-
- Here's a short program I wrote as an example of function pointers. I'd
- appreciate _any_ comments about it.
-
- ---SNIP---
- #include <stdio.h>
-
- typedef void f(int); /* f is a type that is a function taking one int
- * argument and returning an int
- */
- typedef f *ptf; /* ptf is a type that is a pointer to a function */
- typedef ptf aptf[]; /* aptf is a type that is an array of ptfs */
-
- f a, b, c; /* Define three functions a, b and c; each is 'void f(int)' */
-
- int
- main ()
- {
- aptf fa={ a, b, c }; /* Declare and initialise an array of function
- * pointers
- */
- int i;
-
- for(i=0; i<3; i++)
- fa[i](i);
- return 0;
- }
-
- void
- a (int x)
- {
- printf("a: %d\n",x);
- }
-
- void
- b (int y)
- {
- printf("b: %d\n",y);
- }
-
- void
- c (int z)
- {
- printf("c: %d\n",z);
- }
- ---SNIP---
- --
- Wolf a.k.a. Juha Laiho Espoo, Finland
- (GEEK CODE 3.0) GIT d- s+: a- C++ UH++++$ UL++++ P- L+++ E--- W+ N+++ !K w !O
- !M V PS(+) PE Y+ !PGP t- 5? X? R tv- b+ DI? D+ G e+ h!>--- r++ y+
- "...cancel my subscription to the resurrection!" (Jim Morrison)
-